﻿@model Models.Models.InvoiceList
@using WebApp.Classes
@using Models.Responses
@using System.Web.Script.Serialization

@{
    ViewBag.Title = "Invoices";
    var rights = ViewBag.rights;
    string userID = ViewBag.userID.ToString();
    string ppjStoreID = ViewBag.ppjStoreID.ToString();
    
    InvoiceListResponse createdInvoices = new InvoiceListResponse();
    
    if (ViewBag.createdInvoices != null) 
    {
        createdInvoices = (InvoiceListResponse)ViewBag.createdInvoices;
    }
}

@Html.Hidden("userID", userID)
@Html.Hidden("ppjStoreID", ppjStoreID)

<table width="100%">
    <tbody>
        <tr>
            <td width="100%" valign="top">                         
                    <div style="border-style: solid; border-width: 1px; border-color: #DDDDDD"> 
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "StoreName", (Model.SortDirection == "ASC" ? "DESC" : "ASC" ))">Store</a>
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "DomainName", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Domain</a>
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "NumberOfPeriod", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))"># Period</a>                    
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "BillingPeriodName", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Period</a>                                        
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "From", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">From</a>                                                                                
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "To", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">To</a>                                                                                
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "Total", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Total</a>                                                                                
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "BillingRate", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Rate</a>                                                                                
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "BillingUnit", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Unit</a>
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "BillingTypeName", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Type</a>
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "BillingDate", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Date</a>
                            </th>
                            <th>
                                <a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex, "Status", (Model.SortDirection == "ASC" ? "DESC" : "ASC"))">Status</a>
                            </th>
                            <th>
                            </th>
                        </tr>
                        </thead>
                        <tbody>
                        @{
                            foreach (var invoice in Model.List)
                            {
                                var matched = false;
                                
                                if (createdInvoices.InvoiceList != null) 
                                {
                                    matched = createdInvoices.InvoiceList.List.Count(o => o.ID.Equals(invoice.ID)) > 0;
                                }
                                
                                <tr matched='@matched'>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.StoreName</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.DomainName</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.NumberOfPeriod.ToString()</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.BillingPeriodName</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.From.ToString("MM/dd/yyyy")</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.To.ToString("MM/dd/yyyy")</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.Total.ToString("###,###,###.00")</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.BillingRate.ToString()</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.BillingUnit.ToString()</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.BillingTypeName</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.BillingDate.ToString("MM/dd/yyyy")</a>
                                    </td>
                                    <td>
                                        <a href="#" class="modalLink" invoiceid='@invoice.ID'>@invoice.Status</a>
                                    </td>
                                    <td>
                                        <button type="button" class="btn btn-inverse pdf" invoiceid='@invoice.ID' storeid='@invoice.StoreID'><i class="icon-list-alt icon-white"></i> Invoice</button>
                                    </td>
                                </tr>
                            }
                        }
                            <tr>
                                <td colspan="11">
                                    <div class="pagination" style="margin: 0">
                                        <ul>
                                            @{
                                                if (Model.PageIndex > 1)
                                                { 
                                                    <li><a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex - 1, Model.SortColumn, Model.SortDirection)"><i class="icon-arrow-left"></i></a></li>
                                                }

                                                for (var i = 1; i <= WebApp.Classes.Statics.GetPages(Model.ResultCount); i++)
                                                {
                                                    if (i == Model.PageIndex)
                                                    {
                                                        <li class="active"><a href="#">@i</a></li>
                                                    }
                                                    else
                                                    {
                                                        <li><a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", i, Model.SortColumn, Model.SortDirection)">@i</a></li>
                                                    }
                                                }

                                                if (Model.PageIndex != WebApp.Classes.Statics.GetPages(Model.ResultCount) && Model.ResultCount != 0)
                                                {                                                    
                                                    <li><a href="@String.Format("Invoices?bPageIndex={0}&sortColumn={1}&sortDirection={2}", Model.PageIndex + 1, Model.SortColumn, Model.SortDirection)"><i class="icon-arrow-right"></i></a></li>
                                                }                                    
                                            }
                                        </ul>
                                    </div>                        
                                </td>
                                <td colspan="2" style="text-align: right">
                                    <br />
                                    <h5>Records: @Model.ResultCount</h5>
                                </td>
                            </tr>
                        </tbody>                          
                    </table>
                    </div>
            </td>
        </tr>
    </tbody>
</table>      

<div id="viewInvoice" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Invoice # <span id="invoiceNumber"></span> Details</h3>
  </div>
  <div class="modal-body" style="padding-left: 0; padding-right: 0">    
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-inverse" id="charge"><i class="icon-list-alt icon-white"></i> Charge Details</button>
    <button type="button" class="btn btn-inverse" id="void"><i class="icon-ban-circle icon-white"></i> Void</button>
    <button type="button" class="btn btn-inverse" id="csv"><i class="icon-download icon-white"></i> CSV</button>
    <button class="btn btn-inverse" data-dismiss="modal" aria-hidden="true" id="btnClose"><i class="icon-off icon-white"></i> Close</button>
  </div>
</div>

<div id="viewCharge" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Invoice # <span id="invoiceNumber"></span> Charge Details</h3>
  </div>
  <div class="modal-body" style="padding-left: 0; padding-right: 0">    
  </div>
  <div class="modal-footer">
    <button class="btn btn-inverse" data-dismiss="modal" aria-hidden="true" id="btnClose"><i class="icon-off icon-white"></i> Close</button>
  </div>
</div>

<style>
    #viewInvoice    
    {
        position:fixed;
        top: 60%;
        left: 50%;
        width:70em;
        height:30em;
        margin-top: -20em; /*set to a negative number 1/2 of your height*/
        margin-left: -35em; /*set to a negative number 1/2 of your width*/
    }
    #viewCharge    
    {
        position: fixed;
        top: 61%;
        left: 51%;
        width: 68em;
        height: 28em;
        margin-top: -20em;
        margin-left: -35em;
    }    
</style>